/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package progappsproject1;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.awt.Dimension;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class TeacherInterface extends javax.swing.JFrame {

    /**
     * Creates new form TeacherInterface
     */
    public TeacherInterface() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jFormattedTextField1 = new javax.swing.JFormattedTextField();
        jPanel1 = new javax.swing.JPanel();
        mainPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        studentList = new javax.swing.JTable();
        generateReportButton = new javax.swing.JButton();

        jFormattedTextField1.setText("jFormattedTextField1");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 325, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        studentList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Student ID", "Student Name", "PC #", "Time in", "Time out", "Status", "# of Absents", "# of Lates"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Byte.class, java.lang.Object.class, java.lang.Object.class, java.lang.String.class, java.lang.Byte.class, java.lang.Byte.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(studentList);
        studentList.getColumnModel().getColumn(5).setResizable(false);
        studentList.getAccessibleContext().setAccessibleName("");

        generateReportButton.setText("Generate Report");
        generateReportButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generateReportButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 948, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGap(364, 364, 364)
                .addComponent(generateReportButton, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 293, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(generateReportButton, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>                        

    private void generateReportButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                     
        GenerateReport generateReport = new GenerateReport();
        generateReport.setVisible(true);
        generateReport.showReport();
    }                                                    

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TeacherInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public  void run() {
                
                new TeacherInterface().setVisible(true);
                 }
        });
    }
    
    public void addDataToTable(int studId){
     try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/progapps";
            String name = "root";
            String password = "brizzalix";
            try {


           java.sql.Connection con = DriverManager.getConnection(url, name, password);
           java.sql.Statement stmts = null;
           String query = " select studid,concat(lastname,', ',firstname,'  ',middleinitial) as name,pcnumber,timein,"
                          + "timeout,status,numberofabsents,numberoflates from studentinfo";
           stmts = con.createStatement();
           
           if(getCurrentTime() <= 10){
               stmts.executeUpdate("update studentinfo set status='present' where studid =" +studId+"");
           }else if (getCurrentTime() > 10 && getCurrentTime() <= 15 ){
               stmts.executeUpdate("update studentinfo set status='late' where studid =" +studId+"");
               stmts.executeUpdate("update studentinfo set numberoflates=numberoflates+1 where studid =" +studId+"");
           }else{
               stmts.executeUpdate("update studentinfo set status='absent' where studid =" +studId+"");
               stmts.executeUpdate("update studentinfo set numberofabsents=numberofabsents+1 where studid =" +studId+"");
           }
           
           
           
           stmts.executeUpdate("update studentinfo set timein=now() where studid =" +studId+"");
           ResultSet rs = stmts.executeQuery(query);
            rs.next();
            int row = 0;
            while(rs.next()){
                studentList.setValueAt(rs.getString("studid"),row,0);
                studentList.setValueAt(rs.getString("name"),row,1);
                studentList.setValueAt(rs.getString("pcNumber"),row,2);
                studentList.setValueAt(rs.getString("timein"),row,3);
                studentList.setValueAt(rs.getString("timeout"),row,4);
                studentList.setValueAt(rs.getString("status"),row,5);
                studentList.setValueAt(rs.getString("numberOfAbsents"),row,6);
                studentList.setValueAt(rs.getString("numberOfLates"),row,7);

                row++;

    }
            rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
     

    }
    
    void studentLogOut(int studId){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/progapps";
            String name = "root";
            String password = "brizzalix";
            try {


                java.sql.Connection con = DriverManager.getConnection(url, name, password);
                java.sql.Statement stmts = null;

                stmts = con.createStatement();

                stmts.executeUpdate("update studentinfo set timeout=now() where studid =" +studId+"");

            } catch (SQLException e) {
                e.printStackTrace();
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    
    
    int getCurrentTime(){
        Calendar calendar = Calendar.getInstance();
    	calendar.getTime();
    	SimpleDateFormat simpleDate = new SimpleDateFormat("HH:mm:ss");
    	return ( Integer.parseInt(""+simpleDate.format(calendar.getTime()).charAt(3) + simpleDate.format(calendar.getTime()).charAt(4))  );
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton generateReportButton;
    private javax.swing.JFormattedTextField jFormattedTextField1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JTable studentList;
    // End of variables declaration                   


}
